﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using tju.cme.BookStore2024.webapi.Models.Tools;
using tju.cme.BookStore2024.webapi.Models.ViewModels.Chapter;
using tju.cme.BookStore2024.webapi.Services;

namespace tju.cme.BookStore2024.webapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ChapterController(ChapterServices services ): ControllerBase 
    {
        /// <summary>
        /// 新建章节
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //[Authorize(Roles = "Author")]
        [HttpPost("Create/{authorId}")]
        public async Task<IActionResult> Create([FromBody] ChapterCreateModel model, Guid authorId)
        {
            await services.GreateChapterAsync(model, authorId);
            return Ok(new ReturnMessage() { IsSuccess = true });
        }


        /// <summary>
        /// 修改章节
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //[Authorize(Roles = "Author")]
        [HttpPost("Modify/{authorId}")]
        public async Task<IActionResult> Modify([FromBody] ChapterModifyModel model,Guid authorId)
        {
            await services.EditChapterAsync(model, authorId);
            return Ok(new ReturnMessage() { IsSuccess = true });
        }


        /// <summary>
        /// 根据小说ID获取所有章节
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetAllByNovelId/{id}")]
        public async Task<IActionResult> GetAllByNovelId([FromRoute] Guid id)
        {
            return Ok(new ReturnMessage(await services.GetAllByNovelIdAsync(id)));
        }



        /// <summary>
        /// 根据小说ID获取所有已上传章节
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetUploadedByNovelId/{id}")]
        public async Task<IActionResult> GetUploadedByNovelId([FromRoute] Guid id)
        {
            return Ok(new ReturnMessage(await services.GetUploadedByNovelIdAsync(id)));
        }



        /// <summary>
        /// 将章节提交至管理员
        /// </summary>
        /// <param name="chapterId"></param>
        /// <returns></returns>
        //[Authorize(Roles = "Author")]
        [HttpPost("UpToAdmin/{chapterId}/{authorId}")]
        public async Task<IActionResult> UpToAdmin([FromRoute] Guid chapterId, [FromRoute] Guid authorId)
        {
            await services.UpToAdminAsync(chapterId, authorId);
            return Ok(new ReturnMessage() { IsSuccess = true });
        }



        /// <summary>
        /// 管理员审核已提交章节
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //[Authorize(Roles = "Admin")]
        [HttpPost("Examine/{adminId}")]
        public async Task<IActionResult> Examine([FromBody] ChapterExamineModel model, [FromRoute] Guid adminId)
        {
            await services.ExamineChapterAsync(model, adminId);
            return Ok(new ReturnMessage() { IsSuccess = true });
        }

        /// <summary>
        /// 管理员将审核通过的章节上传到该小说
        /// </summary>
        /// <param name="chapterId"></param>
        /// <returns></returns>
        //[Authorize(Roles = "Admin")]
        [HttpPost("UpToBookStore/{chapterId}/{adminId}")]
        public async Task<IActionResult> UpToBookStore([FromRoute] Guid chapterId, [FromRoute] Guid adminId)
        {
            await services.UploadAsync(adminId, chapterId);
            return Ok(new ReturnMessage() { IsSuccess = true });
        }



    }
}
